package com.dmbjz.actuator.health;


import com.dmbjz.actuator.entity.Person;
import jakarta.annotation.Resource;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;


/**
 * 做健康监控
 *
 * 1、实现 HealthIndicator 接口定制组件的健康状态对象
 * 2、
 */
@Component
public class MyHealthIndicator2 extends AbstractHealthIndicator {

    @Resource
    private Person person;

    /** 做健康检查 */
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {

        //调用方法，判断组件是否存活
        int health = person.checkHealth();

        //根据状态设置不同的信息
        if(health == 1){
            builder.up()
                    .withDetail("msg", "ok service")
                    .withDetail("code", "200");
        }else{
            builder.down()
                    .withDetail("msg", "error service")
                    .withDetail("code", "500")
                    .withException(new RuntimeException());
        }

        builder.build();

    }

}
